/*---------------------------------------------------------------------------*/
#include "FileOps.h"
#include "FileNode.h"
#include <QClipboard>
#include "Utils.h"
#include <QApplication>
#include <QString>
#include <QStringList>
#include <cstdlib>
#include <QMessageBox>
/*---------------------------------------------------------------------------*/
FileOps::FileOps( )
{

}
/*---------------------------------------------------------------------------*/
FileOps::~FileOps()
{
	
}
/*---------------------------------------------------------------------------*/
void FileOps::cut( vector<FileNode *> files )
{
	QString str = "gfm.cut:";
	
	foreach(FileNode*, file, files)
	{
		str+= Utils::escapeChars( (*file)->getAbsolutePath() ).c_str();
		str+=";";
	}
	
	
	QApplication::clipboard()->setText(str);
}
/*---------------------------------------------------------------------------*/
void FileOps::copy( vector<FileNode *> files )
{
	QString str = "gfm.copy:";
	
	foreach(FileNode*, file, files)
	{
		str+= Utils::escapeChars( (*file)->getAbsolutePath() ).c_str();
		str+=";";
	}
	
	QApplication::clipboard()->setText(str);
}
/*---------------------------------------------------------------------------*/
void FileOps::paste( FileNode *dest )
{
	QString str = QApplication::clipboard()->text();

	
	if( str.left(4) != "gfm." ) return;
	
	if( str.left(7) == "gfm.cut" )
	{
		QStringList data = str.mid(8).split(";");
		
		
		for(int i=0; i < data.count(); i++)
		{
			if( data[i] == "" ) break;
			
			string cmd;
			
			cmd = "mv ";
			cmd += data[i].toStdString();
			cmd += " ";
			cmd += Utils::escapeChars( dest->getAbsolutePath() );
			cmd += " &";
			
			system(cmd.c_str());
			DEBUGMSG("FileOps::paste() : " << cmd );
		}
	
	}
	
	if( str.left(8) == "gfm.copy" )
	{
		QStringList data = str.mid(9).split(";");
		
		for(int i=0; i < data.count(); i++)
		{
			if( data[i] == "" ) break;
			
			string cmd;
			
			cmd = "cp ";
			cmd += data[i].toStdString();
			cmd += " ";
			cmd += Utils::escapeChars( dest->getAbsolutePath() );
			cmd += "/ &";
			
			system(cmd.c_str());
			DEBUGMSG("FileOps::paste() : " << cmd );
		}
			
	}	
	


}
/*---------------------------------------------------------------------------*/
void FileOps::remove( vector<FileNode *> files )
{
	QString strfiles = "Are you sure to delete these files ? \n\n";
	foreach(FileNode*, file, files)
	{
		strfiles+= (*file)->getName().c_str();
		strfiles+="\n";
	}	
	
 	if( QMessageBox::question( NULL, "gfm: delete confirmation", strfiles, QMessageBox::Yes, QMessageBox::No) == QMessageBox::Yes)
 	{
 	
		foreach(FileNode*, file, files)
		{
			string cmd = "rm -rf ";
			cmd += (*file)->getAbsolutePath();
			cmd += " &";
			system(cmd.c_str());
			DEBUGMSG("FileOps::remove() : " << cmd );
		} 	
 	
 		
 	}
}
/*---------------------------------------------------------------------------*/
